package statisticAnalysis;

import DL.Map3DTo2D;
import DL.SPos;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwj on 2018/3/26.
 */
public class viewportMovement2 {


    final static String csvPath = "J:\\360dataset\\sensory\\orientation"; // csv路径


    public static File[] getCsvFileNames() {
        File f = new File(csvPath);
        if (!f.exists()) {
            System.out.println(csvPath + " not exists");
            return null;
        }

        File listFiles[] = f.listFiles();
        return listFiles;
    }

    public static int getGapPos(int xGap){
        // offset 0~10 10~30 30~60 60~90 90~120 大于120
        int x1=0;
        if(xGap>=0&&xGap<10) x1=0;
        else if(xGap>=10&&xGap<30) x1=1;
        else if(xGap>=30&&xGap<60) x1=2;
        else if(xGap>=60&&xGap<90) x1=3;
        //else if(xGap>=90&&xGap<120) x1=4;
        else if(xGap>=90) x1=5;
        return x1;
    }

    public static void main(String[] args) {
        File[] listFiles=getCsvFileNames();
        List<SPos> sPosList;
        int xGap=0,x1=0,x2=0;
        double predictOffset=0;
        int[] count6=new int[6];
        int[] count6offset=new int[6];
        int[] count6sumoffset=new int[6];
// offset 0~10 10~30 30~60 60~90 90~120 大于120
        int[][] movement66=new int[6][6];
        long allCount6=0,allCount6offset=0,count=0,allCount6sumoffset=0;
        double alpha,beta;
        for(File csv:listFiles) {
            //System.out.println("开始读取" + csv.getName());
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(csv));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            sPosList=new ArrayList<SPos>(2048);
            try {
                String line = br.readLine();
                while ((line = br.readLine()) != null)  //读取到的内容给line变量
                {
                    String[] infos=line.split(",");
                    double x=Double.parseDouble(infos[1]);
                    double y=Double.parseDouble(infos[2]);
                    double z=Double.parseDouble(infos[3]);
                    sPosList.add(Map3DTo2D.map3DTo2D(new SPos(x,y,z),360,180));
                }
                //System.out.println("读取完" + csv.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }
            /*
            for(SPos sPos:sPosList){
                System.out.println(csv.getName()+"  --  "+sPos.x+" "+sPos.y+" "+sPos.z);
            }
            */
            int offsetFrame=30,gapFrame=30;
            predictOffset=0;
            for(int i=30;i<sPosList.size()-60;i++){
                try {
                    //预测为止 P（i）=P（i+offsetFrame）
                    for (offsetFrame=30;offsetFrame<60;offsetFrame++) {
                        xGap = (int) Math.abs(sPosList.get(i).x - sPosList.get(i + offsetFrame).x);
                        xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                        allCount6 += xGap;
                        // offset 0~10 10~30 30~60 60~90 90~120 大于120
                        x1 = getGapPos(xGap);
                        count6[x1]++;
                    }

                    //xGap = (int) Math.abs(sPosList.get(i + offsetFrame).x - (5*(sPosList.get(i).x - sPosList.get(i - offsetFrame).x) + sPosList.get(i).x));
                    //p即i-offsetFrame到i的偏移量
                    beta=0.5;
                    int p=(int)(sPosList.get(i).x - sPosList.get(i - gapFrame).x);
                    for(offsetFrame=30;offsetFrame<60;offsetFrame++) {
                        xGap = (int) Math.abs(sPosList.get(i + offsetFrame).x - (beta * p + sPosList.get(i).x));
                        xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                        allCount6offset += xGap;
                        x2 = getGapPos(xGap);
                        count6offset[x2]++;
                    }

                    alpha=0.1;
                    beta=0.4;
                    xGap= (int) (sPosList.get(i).x - sPosList.get(i - gapFrame).x);
                    predictOffset=beta*xGap+alpha*predictOffset;
                    for(offsetFrame=30;offsetFrame<60;offsetFrame++) {
                        xGap = (int) Math.abs(sPosList.get(i + offsetFrame).x - (predictOffset + sPosList.get(i).x));
                        xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                        allCount6sumoffset += xGap;
                        x2 = getGapPos(xGap);
                        count6sumoffset[x2]++;
                    }
                    count+=30;
                }catch (Exception e){
                    System.out.println(xGap);
                    System.out.println(e.toString());
                }
            }

        }
        System.out.println("count6 ");
        for(int i=0;i<6;i++)
            System.out.println(count6[i]);
        System.out.println("-----------");
        System.out.println("count6offset ");
        for(int i=0;i<6;i++)
            System.out.println(count6offset[i]);
        System.out.println("-----------");
        System.out.println("count6sumoffset ");
        for(int i=0;i<6;i++)
            System.out.println(count6sumoffset[i]);
        System.out.println("-----------");
        System.out.println((allCount6+0.0)/count);
        System.out.println((allCount6offset+0.0)/count);
        System.out.println((allCount6sumoffset+0.0)/count);

    }


}
